{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#文件操作\n",
    "#1.打开文件\n",
    "#f=open(\"test.txt\",\"r\") #相对路径访问\n",
    "f=open(r\"D:\\python\\进阶篇\\test.txt\",\"a+\") #绝对路径访问 r 只读 r+ 读写(覆盖) w 写入(覆盖，不存在就先创建) w+ 读写(覆盖) a 追加 a+ 读写 追加\n",
    "# print(f.name) #返回文件名\n",
    "# print(f.mode) #返回打开模式\n",
    "# print(f.encoding) #返回编码\n",
    "# print(f.closed) #返回文件是否关闭\n",
    "#2.读写文件\n",
    "# a=f.read() #读取文件全部内容\n",
    "# print(a)\n",
    "# b=f.read(5) #读取前10个字符\n",
    "# print(b)\n",
    "# while True:\n",
    "#     a=f.readline() #读取一行\n",
    "#     if not a:\n",
    "#         break\n",
    "#     print(a)\n",
    "# d=f.readlines() #读取所有行，返回列表\n",
    "# print(d)\n",
    "# f.write(\"\\n我是追加的内容\")\n",
    "# #文件定位 tell()返回文件指针位置  seek(offset,whence=0) 设置文件指针位置 offset:偏移量  whence:0-文件开头  1-当前位置  2-文件结尾\n",
    "# print(f.tell())\n",
    "# f.seek(0,0)\n",
    "# d=f.read()\n",
    "# print(d)\n",
    "# #3.关闭文件\n",
    "# f.close()\n",
    "#4.文件操作模式\n",
    "#5.文件对象的方法\n",
    "#6.文件操作的其他函数\n",
    "#7.文件操作实例\n",
    "# with open(\"test.txt\",\"r\") as f: #with语句自动调用close()方法\n",
    "# with open(\"test.txt\",\"a+\",encoding=\"utf-8\") as f:\n",
    "#     f.write(\"\\n我是追加的内容\")\n",
    "#     f.seek(0,0)\n",
    "#     d=f.read()\n",
    "#     print(d)\n",
    "#读取图片\n",
    "# img=b\"\"\n",
    "# with open(r\"D:\\python\\123.jpeg\",\"rb\") as f:\n",
    "#     img=f.read()\n",
    "#     print(img)\n",
    "# #写入图片\n",
    "# with open(r\"D:\\python\\进阶篇\\1223.jpeg\",\"wb\") as f:\n",
    "#     f.write(img)\n",
    "#     print(img)\n",
    "# os 模块\n",
    "import os\n",
    "# os.getcwd() #获取当前工作目录\n",
    "print(os.getcwd())\n",
    "# os.chdir(\"D:\\python\\进阶篇\") #修改当前工作目录\n",
    "# os.mkdir(\"test\") #创建目录\n",
    "# os.rmdir(\"test\") #删除目录\n",
    "# os.rename(\"test\",\"test2\") #重命名目录\n",
    "print(os.listdir()) #获取目录下的文件列表\n",
    "print(os.listdir(\"../\")) #获取上一级目录下的文件列表\n",
    "# os.path.join(\"D:\\python\\进阶篇\",\"test\") #拼接路径\n",
    "# os.path.split(\"D:\\python\\进阶篇\\test\") #分割路径\n",
    "# os.remove(\"test.text\")  #删除文件"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
